package com.jingding.procurement.schedule;

import com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector;
import com.jingding.procurement.controller.ProcureMarketController;
import com.jingding.procurement.enumeration.EntityEnum;
import com.jingding.procurement.pojo.entity.Entity;
import com.jingding.procurement.pojo.entity.PurchaseOrdersEntity;
import com.jingding.procurement.util.IO;
import com.jingding.procurement.util.TimeFormat;
import com.jingding.procurement.web.JsonResult;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 3.采购订单接口定时任务
 *
 */
@Data
@Slf4j
@Component
@PropertySource("classpath:/task-config.ini")

public class PurchaseOrders implements SchedulingConfigurer {

    @Value("${printTime.cron}")
    private String cron;
    @Autowired
    private ProcureMarketController procureMarketController;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 动态使用cron表达式设置循环间隔
        taskRegistrar.addTriggerTask(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
                LocalDateTime now = LocalDateTime.now();
                PurchaseOrdersEntity read = IO.purchaseOrdersRead();
                //进行定时时间对比
                if (now.format(dateTimeFormatter).equals(read.getTaskTime())||now.format(dateTimeFormatter).equals(read.getFirstTime())){
                    //执行推送任务
                    log.debug("[采购订单]定时推送任务开始执行,执行时间:{} ,查询开始时间:{} ,查询结束时间:{}",TimeFormat.secondTime(),read.getStartTime(),TimeFormat.secondTime());
                    Entity entity =new Entity();
                    entity.setPort(EntityEnum.POE.getCode());
                    entity.setStartTime(read.getStartTime());
                    entity.setEndTime(TimeFormat.secondTime());
                    procureMarketController.pushPurchaseOrders(entity);

                }
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 使用CronTrigger触发器，可动态修改cron表达式来操作循环规则
                CronTrigger cronTrigger = new CronTrigger(cron);
                return cronTrigger.nextExecutionTime(triggerContext);
            }
        });
    }

}
